{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "eaeaf3fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detOfMtr(x):\n",
    "    #print(f'x是{x}')\n",
    "    if type(x)!=list:\n",
    "        return \"输入格式错误——不是列表\"\n",
    "    elif 0 in [0 for i in range(len(x)) if type(x[i])!=list]:\n",
    "        return \"输入格式错误——不是矩阵\"\n",
    "    elif 0 in [0 for i in range(len(x)) if len(x[i])!=len(x[0])]:\n",
    "        return \"输入格式错误——不是矩阵\"\n",
    "    elif 0 in [0 for i in range(len(x)) if len(x)!=len(x[0])]:\n",
    "        return \"输入格式错误——不是方阵\"\n",
    "    elif len(x)==1:\n",
    "        return x[0][0]\n",
    "    elif len(x)==2:\n",
    "        return x[0][0]*x[1][1]-x[0][1]*x[1][0]\n",
    "    else:\n",
    "        det=sum(((-1)**c)*x[0][c]*detOfMtr([row[:c]+row[c+1:] for row in x[1:]]) for c in range(len(x)))\n",
    "        #det=0\n",
    "        #for c in range(len(x)):\n",
    "            #minor=[row[:c]+row[c+1:] for row in x[1:]]\n",
    "            #det+=((-1)**c)*x[0][c]*detOfMtr(minor)\n",
    "        return det"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e446516e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-3"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detOfMtr([[1,2,4],[4,5,6],[7,8,9]])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
